package com.caifu.excel.shopExcel;

import com.alibaba.excel.write.handler.AbstractRowWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.caifu.enums.LabelTypeEnum;
import com.caifu.service.ReportRecordService;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;

/**
 * ClassName :CommentWriteHandler
 *
 * @author :  yb
 * @description ：  批注
 * @date : 2020-09-08 13:05
 */
public class ShopCommentWriteHandler extends AbstractRowWriteHandler {

    private ReportRecordService reportRecordService;

    public ShopCommentWriteHandler(ReportRecordService reportRecordService) {
        this.reportRecordService = reportRecordService;
    }


    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row,
                                Integer relativeRowIndex, Boolean isHead) {
        //社区身份标签
        String[] shopTypeArr = reportRecordService.getLabelNameArray(LabelTypeEnum.SPBQ.getKey());


        String shopTypeStr = getStr(shopTypeArr);

        if (isHead) {
            Sheet sheet = writeSheetHolder.getSheet();
            Drawing<?> drawingPatriarch = sheet.createDrawingPatriarch();
            //商铺标签 批注
            setRowRemark(drawingPatriarch, sheet, shopTypeStr, 15);
        }
    }


    private void setRowRemark(Drawing<?> drawingPatriarch, Sheet sheet, String strRemark, int cellNum) {
        //  倒数2 3个参数 批注所占大小  最后两个数字为宽 高
        Comment comment =
                drawingPatriarch.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, cellNum, 0, cellNum + 3, cellNum));

        // 输入批注信息
        comment.setString(new XSSFRichTextString(strRemark));
        // 将批注添加到单元格对象中
        sheet.getRow(0).getCell(cellNum).setCellComment(comment);
    }

    private String getStr(String[] arr) {
        return String.join(",", arr);
    }

}
